/**
 * @file Define the LogService class in this module
 * 
 * @author Steve Gao<gaofu@parllay.com>
 */
const winston = require('winston');

const BaseService     = require('./base-service').BaseService;
const {Configuration}   = require('../constants');
const utils           = require('../utils');
/**
 * The log utility class, this class is used for ouputing information to file
 * 
 * @class LogService
 */
class LogService extends BaseService {

    get loggerConfig() {
        return this.getApplicationConfiguration(Configuration.LOG);
    }

    constructor(context) {
        super(context);
        this.setupWinstonLogger();
    }

    setupWinstonLogger() {
        if (this.loggerConfig.enable_console_logging) {
            this.setupConsoleLogger(this.loggerConfig.console_logger);
        }

        if (this.loggerConfig.enable_file_logging) {
            this.setupFileLoggers(this.loggerConfig.file_loggers);
        }
    }

    setupConsoleLogger(options) {
        winston.remove(winston.transports.Console);
        winston.add(winston.transports.Console, options);
    }

    setupFileLoggers(files) {
        utils.checkArrayParameter(files, 'files');

        files.forEach(function(options) {
            winston.add(winston.transports.File, options);
        });
    }

    getLogger() {
        return winston;
    }
}

exports.LogService = LogService;